/*
 * Copyright 2015 Diogo Bernardino
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.herui.iot.utils;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

import androidx.annotation.NonNull;

public class ViewTools {


    /**
     * Converts dp size into pixels.
     *
     * @param dp dp size to get converted
     * @return Pixel size
     */
    public static float fromDpToPx(float dp) {
        return dp * Resources.getSystem().getDisplayMetrics().density;
    }

    /**
     * convert sp to its equivalent px
     *
     * 将sp转换为px
     */
    public static int sp2px(Context context, float spValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }

    /**
     * Converts a {@link Drawable} into {@link Bitmap}.
     *
     * @param drawable {@link Drawable} to be converted
     * @return {@link Bitmap} object
     */
    public static Bitmap drawableToBitmap(@NonNull Drawable drawable) {

        if (drawable instanceof BitmapDrawable) {
            return ((BitmapDrawable) drawable).getBitmap();
        }

        Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable
                .getIntrinsicHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);

        return bitmap;
    }


    /**
     * Find the Greatest Common Denominator.
     * https://en.wikipedia.org/wiki/Euclidean_algorithm
     *
     * @param min Mininum value
     * @param max Maximum value
     * @return Greatest common denominator
     */
    public static int GCD(int min, int max) {
        return max == 0 ? min : GCD(max, min % max);
    }

    /**
     * 使用java正则表达式去掉多余的.与0,并且最终结果四舍五入取整
     *
     * @return
     */
    public static String subZeroAndDot(float data) {
        String s = String.format("%.1f", data);
        if (s.indexOf(".") > 0) {
            s = s.replaceAll("0+?$", "");//去掉多余的0
            s = s.replaceAll("[.]$", "");//如最后一位是.则去掉
            s = (int) Math.rint(Double.parseDouble(s)) + "";//四舍五入取整
        }
        return s;
    }

    /**
     * 获取字符高度
     *
     * @param paint
     * @return
     */
    public static int getTextHeigh(Paint paint) {
        Paint.FontMetrics fm = paint.getFontMetrics();
        return (int) (fm.descent - fm.ascent);
    }

    /**
     * 获取字符宽度
     *
     * @param paint
     * @param text
     * @return
     */
    public static int getTextWidth(Paint paint, String text) {
        return (int) (paint.measureText(text));
    }

    /**
     * 获取绘制texty轴位置
     * @param paint
     * @param centerY   y轴中心位置
     * @return
     */
    public static float getTextCenterY(Paint paint, float centerY) {
        Paint.FontMetrics fm = paint.getFontMetrics();
        return centerY - fm.top / 2 - fm.bottom / 2;
    }

    /**
     * 绘制长的path
     *
     * @param canvas
     * @param paint
     * @param x1
     * @param x2
     * @param y
     */
    public static void drawLongXLinePath(Canvas canvas, Paint paint, float x1, float x2, float y) {
        float i = x1;
        for (; i < x2; i += 100) {
            Path path = new Path();
            path.moveTo(x1, y);
            path.lineTo(i, y);
            canvas.drawPath(path, paint);
        }
        Path path = new Path();
        path.moveTo(i, y);
        path.lineTo(x2, y);
        canvas.drawPath(path, paint);
    }

}
